package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Medicine_dao;
import com.clinic.domain.Medicine;
import com.clinic.util.Util_imp;

public class Medicine_imp implements Medicine_dao {

	SessionFactory session_factory = null;

	public Medicine_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_medicine(Medicine m) {
		System.out.println("Enter - create_medicine");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int me_obj_id = 0;
		try {
			tx = session.beginTransaction();
			me_obj_id = (Integer) session.save(m);
			tx.commit();
			System.out.println("New Medicine Created with ID: " + me_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Medicine Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_medicine");
		return me_obj_id;
	}

	@Override
	public void update_medicine(Medicine me) {
		System.out.println("Enter - update_medicine");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(me);
			tx.commit();
			System.out.println("Medicine Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Medicine Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_medicine");
	}

	@Override
	public void delete_medicine(int me_obj_id) {
		System.out.println("Enter - delete_medicine");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Medicine m = (Medicine) session.get(Medicine.class, me_obj_id);
			session.delete(m);
			tx.commit();
			System.out.println("Medicine with ID: " + me_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Medicine Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_medicine");
	}

	@Override
	public Medicine get_medicine(int me_obj_id) {
		System.out.println("Enter - get_medicine");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Medicine m = null;
		try {
			tx = session.beginTransaction();
			m = (Medicine) session
					.createQuery("from Medicine where me_obj_id=:id")
					.setParameter("id", me_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Name: " + m.getName());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_medicine");
		return m;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Medicine> get_all_medicines() {
		System.out.println("Enter - get_all_medicines");
		Session session = session_factory.openSession();
		List<Medicine> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Medicine").list();
			tx.commit();
			System.out.println("Number of Medicines returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_medicines");
		return l;
	}

	@Override
	public Medicine persistOrMerge(Medicine m) {
		return (Medicine) session_factory.getCurrentSession().merge(m);
	}

	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Medicine").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Medicine: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}